package com.jiangxjsy.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.jiangxjsy.admin.mapper.AdminConfigMapper;
import com.jiangxjsy.admin.property.JwtProperty;
import com.jiangxjsy.admin.service.AdminConfigService;
import com.jiangxjsy.core.common.AdminModule;
import com.jiangxjsy.core.constant.BasicConstant;
import com.jiangxjsy.core.exception.AuthorizationException;
import com.jiangxjsy.core.jwt.JwtCore;
import com.jiangxjsy.core.wrap.ResultCode;
import com.jiangxjsy.model.admin.bo.AdminCompanyBO;
import com.jiangxjsy.model.admin.entity.AdminConfig;
import com.jiangxjsy.model.admin.entity.AdminModelSort;
import com.jiangxjsy.model.admin.vo.ModuleSettingVO;
import com.jiangxjsy.model.core.entity.UserMeta;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * wukong-crm-master
 * 蒋胜宇
 * 2025/6/6 16:27
 */

@Service
public class AdminConfigServiceImpl implements AdminConfigService {
    @Resource
    private AdminConfigMapper adminConfigMapper;

    @Resource
    private JwtProperty jwtProperty;

    /**
     * 根据名称查询配置
     *
     * @param name 配置名称
     * @return 配置
     */
    @Override
    public AdminConfig queryConfigByName(String name) {
        return adminConfigMapper.selectByName(name);
    }

    /**
     * 查询公司信息
     *
     * @return 公司信息
     */
    @Override
    public AdminCompanyBO queryAdminConfig() {
        AdminConfig adminConfig = adminConfigMapper.selectByName("companyInfo");
        AdminCompanyBO adminCompanyBO = new AdminCompanyBO();
        if (adminConfig != null) {
            adminCompanyBO = JSON.parseObject(adminConfig.getValue(), AdminCompanyBO.class);
        }
        return adminCompanyBO;
    }

    /**
     * 查询多个配置
     *
     * @param names 配置名称
     * @return 配置列表
     */
    @Override
    public List<AdminConfig> queryConfigListByName(Object... names) {
        String[] array = Arrays.stream(names).map(Object::toString).toArray(String[]::new);
        return adminConfigMapper.selectByNames(Arrays.asList(array));
    }

    /**
     * 查询模块设置
     *
     * @return 模块设置列表
     */
    @Override
    public List<ModuleSettingVO> queryModuleSetting() {
        List<AdminConfig> adminConfigList = queryConfigListByName((Object[]) AdminModule.getValues());
        List<ModuleSettingVO> moduleSettingList = new ArrayList<>();
        adminConfigList.forEach(adminConfig -> {
            ModuleSettingVO moduleSettingVO = new ModuleSettingVO();
            moduleSettingVO.setSettingId(Math.toIntExact(adminConfig.getSettingId()));
            moduleSettingVO.setModule(adminConfig.getName());
            moduleSettingVO.setStatus(Math.toIntExact(adminConfig.getStatus()));
            moduleSettingVO.setType(adminConfig.getValue());
            moduleSettingVO.setName(adminConfig.getDescription());
            moduleSettingList.add(moduleSettingVO);
        });
        return moduleSettingList;
    }

    /**
     * 查询头部排序
     *
     * @param request 请求
     * @param response 响应
     * @return 头部排序
     */
    @Override
    public List<String> queryHeaderModelSort(HttpServletRequest request, HttpServletResponse response) {
        String token = request.getHeader(BasicConstant.TOKEN_NAME);
        UserMeta userMeta;
        try {
            userMeta = new JwtCore<>(UserMeta.class).analyzeToken(token, jwtProperty.getPublicKey());
        } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException | InstantiationException e) {
            throw new AuthorizationException(ResultCode.AuthorizationResultCode.AUTHORIZATION_LOGIN_ERR);
        }
        Long userId = userMeta.getUserId();

        List<AdminModelSort> list = adminConfigMapper.selectByTypeAndUserId(1, userId);
        return list.stream().map(AdminModelSort::getModel).collect(Collectors.toList());
    }
}
